from typing import List

import pytest
import yaml

from pytestwork.Calculator import Calculator


@pytest.fixture(scope='class')
def initadd_class():
    # setup
    print("加法计算开始")
    calcadd = Calculator()
    yield calcadd
    # setup
    print("加法计算结束")


@pytest.fixture(scope='class')
def initdiv_class():
    # setup
    print("除法计算开始")
    calcdiv = Calculator()
    yield calcdiv
    print("除法计算结束")


def get_datas():
    with open("datas/calc.yaml") as f:
        datas = yaml.safe_load(f)
    return datas


@pytest.fixture(params=get_datas()['add_int_small_datas'], ids=get_datas()['add_int_small_ids'])
def get_add_int_small_datas(request):
    return request.param


@pytest.fixture(params=get_datas()['add_int_big_datas'], ids=get_datas()['add_int_big_ids'])
def get_add_int_big_datas(request):
    return request.param


@pytest.fixture(params=get_datas()['add_float_datas'], ids=get_datas()['add_float_ids'])
def get_add_float_datas(request):
    return request.param


@pytest.fixture(params=get_datas()['div_int_small_datas'], ids=get_datas()['div_int_small_ids'])
def get_div_int_small_datas(request):
    return request.param


@pytest.fixture(params=get_datas()['div_int_big_datas'], ids=get_datas()['div_int_big_ids'])
def get_div_int_big_datas(request):
    return request.param


@pytest.fixture(params=get_datas()['div_float_datas'], ids=get_datas()['div_float_ids'])
def get_div_float_datas(request):
    return request.param


def pytest_collection_modifyitems(session, config, items: List):
    print("这是收集所有测试用例的方法")
    print(items)
    for item in items:
        # 拿到测试用例的名字，对它进行重编码，编码成utf-8的格式（utf-8是支持所有格式的）
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')
